package com.huang.educms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huang.educms.entity.CrmBanner;
import com.huang.educms.service.CrmBannerService;
import com.hunag.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-27
 */
@RestController
@RequestMapping("/educms/crm-bannerAdmin")
public class BannerAdminController {
    @Autowired
    private CrmBannerService service;

    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable Integer page,
                        @PathVariable Integer limit) {

        Page<CrmBanner> page1 = new Page<>(page, limit);
        IPage<CrmBanner> page2 = service.page(page1, null);
        List<CrmBanner> records = page2.getRecords();
        long total = page2.getTotal();
        return R.ok().data("total", total).data("records", records);

    }

    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        service.save(crmBanner);
        return R.ok();
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public R updateBannerById(@RequestBody CrmBanner banner) {
        service.updateById(banner);
        return R.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public R removeBanner(@PathVariable String id) {
        service.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public R getBanner(@PathVariable String id) {
        CrmBanner banner = service.getById(id);
        return R.ok().data("item", banner);
    }

}

